package com.liukai.java_jxgl.service.impl;

import com.liukai.java_jxgl.mapper.IStudentMapper;
import com.liukai.java_jxgl.model.QueryInfo;
import com.liukai.java_jxgl.model.Students;
import com.liukai.java_jxgl.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private IStudentMapper iStudentMapper;

    /**
     * 获取所有学生信息
     *
     * @return
     */
    @Override
    public List<Students> queryAllStudent() {
        return iStudentMapper.getAllStudents();
    }

    /**
     * 条件分页查询
     *
     * @param queryInfo
     * @return
     */
    @Override
    public List<Students> queryAllStudentByInfo(QueryInfo queryInfo) {
        Object query = queryInfo.getQuery();
        if (query != null)
            query = "%" + query + "%";// 拼接模糊查询字符串
        String type = queryInfo.getType();
        int pageStart = queryInfo.getPageNum(); // 第几页
        int pageSize = queryInfo.getPageSize(); // 每页数据个数

        int count = iStudentMapper.getStudentsCount(query, type); // 该查询条件下的学生个数
        int startNum = 0; // 第几条数据开始查询
        int endNum = 0; // 第几条数据结束查询
        System.out.println("学生个数：" + count);

        int pageCount = count / pageSize + 1; // 获取当前条件下最多能分几页
        System.out.println("pageStart:" + pageStart);
        System.out.println("pageCount:" + pageCount);
        if (pageStart > pageCount) return null; // 第几页大于总页数，返回null
        startNum = (pageStart - 1) * pageSize + 1;
        if (pageStart == pageCount)
            endNum = count;
        else
            endNum = startNum + pageSize - 1;
        System.out.println(startNum + ":" + endNum);
//        COURSES
        return iStudentMapper.getAllStudentsByInfo(query, type, startNum, endNum);
    }

    /**
     * 新增一条学生信息
     *
     * @param students
     * @return
     */
    @Override
    public int addStudent(Students students) {

        if (studentIsNull(students)) return -1;

        int i = iStudentMapper.insertStudent(students);
        return i > 0 ? i : 0;
    }

    @Override
    public int updateStudent(Students students) {


        return iStudentMapper.updateStudent(students);
    }

    @Override
    public int getStudentsCount(Object query, String type) {
        if (query != null)
            query = "%" + query + "%";
        return iStudentMapper.getStudentsCount(query, type);
    }

    @Override
    public Students findStudentById(Long studentId) {
        return iStudentMapper.findStudentById(studentId);
    }


    private boolean studentIsNull(Students students) {
        Long studentId = students.getStudentId();
        String studentName = students.getStudentName();
        String studentSex = students.getStudentSex();
        Date studentBirth = students.getStudentBirth();
        String studentNationality = students.getStudentNationLity();
        String studentDeptId = students.getStudentDeptId();
        String studentClassId = students.getStudentClassId();
        if (studentId == null || studentName.equals("")
                || studentSex.equals("") || studentBirth.equals("")
                || studentNationality.equals("")) {
            return true;
        }
        return false;
    }

}
